﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using Contracts.Entities;
using Model.MovieElements;
using Contracts.Library;

namespace Model.Movie
{
    public static class MovieItemConverter
    {
        public static IEnumerable<MovieItem> GetElements(EntityCollection<Movies> movies)
        {
            var results = new List<MovieItem>();
            foreach (var entity in movies)
                results.Add(GetItem(entity));

            return results;
        }

        public static IEnumerable<MovieItem> GetLookupItems(EntityCollection<Movies> movies)
        {
            var results = new List<MovieItem>();
         
            foreach (var entity in movies)
                results.Add(GetLookupItem(entity));

            return results;
        }

        /// <summary>
        /// Lookup Item mit null werten
        /// </summary>
        public static MovieItem GetLookupItem(Movies movie)
        {
            if (movie == null)
                return new MovieItem();

            var movieItem = new MovieItem();
            movieItem.Id = movie.Id;
            movieItem.Name = movie.Name;
            movieItem.Quality = movie.Quality;
            movieItem.FolderPath = movie.FolderPath;
            movieItem.RuntimeMin = (short)movie.RuntimeMin;
            movieItem.ReleaseYear = (short)movie.ReleaseYear;
            movieItem.FskAge = (Int16)movie.FSKAge;
            movieItem.Rating = (Int16)movie.Rating;
            movieItem.TmdbRating = (double)movie.TmdbRating / 10;
            movieItem.Revenue = movie.Revenue;
            movieItem.Budget = movie.Budget;
            movieItem.TrailerUrl = movie.TrailerUrl;
            movieItem.Cover = movie.Cover;
            //movieItem.Watched = movie.Watched == new byte[1] { 1 };

            return movieItem;
        }

        public static MovieItem GetItem(Movies movie)
        {
            if (movie == null)
                return new MovieItem();
            
            var movieItem = new MovieItem();
            movieItem.Id = movie.Id;
            movieItem.Name = movie.Name;
            movieItem.OriginalName = movie.OriginalName;
            movieItem.Quality = movie.Quality;
            movieItem.Overview = movie.Overview;
            movieItem.FolderPath = movie.FolderPath;
            movieItem.RuntimeMin = (short)movie.RuntimeMin;
            movieItem.ReleaseYear = (short)movie.ReleaseYear;
            movieItem.FskAge = (Int16)movie.FSKAge;
            movieItem.Rating = (Int16)movie.Rating;
            movieItem.TmdbRating = (double)movie.TmdbRating/10;
            movieItem.Revenue = movie.Revenue;
            movieItem.Budget = movie.Budget;
            movieItem.TrailerUrl = movie.TrailerUrl;
            movieItem.Cover = movie.Cover;
            movieItem.Actors = ActorConverter.GetElements(movie.Actors);
            movieItem.Directors = DirectorConverter.GetElements(movie.Directors);
            movieItem.Genres = GenreConverter.GetElements(movie.Genres);
            movieItem.Countries = CountryConverter.GetElements(movie.Countries);
            movieItem.Studios = StudioConverter.GetElements(movie.Studios);
            movieItem.Backdrops = BackdropsConverter.GetEntities(movie.Backdrops);
            //movieItem.Watched = movie.Watched == new byte[1] { 1 };

            return movieItem;
        }

        public static Movies GetEntity(MovieItem item)
        {
            if (item == null)
                return new Movies();

            var entity = new Movies();
            entity.Id = item.Id;
            entity.Name = item.Name;
            entity.OriginalName = item.OriginalName;
            entity.Quality = item.Quality;
            entity.Overview = item.Overview;
            entity.FolderPath = item.FolderPath;
            entity.RuntimeMin = item.RuntimeMin;
            entity.ReleaseYear = item.ReleaseYear;
            entity.FSKAge = item.FskAge;
            entity.Rating = (short)item.Rating;
            entity.TmdbRating = Convert.ToInt16(item.TmdbRating);
            entity.Revenue = item.Revenue;
            entity.Budget = item.Budget;

            entity.TrailerUrl = item.TrailerUrl;
            entity.Cover = item.Cover;

            if (item.Actors != null)
                entity.Actors = ActorConverter.GetEntities(item.Actors);

            if (item.Directors != null)
                entity.Directors = DirectorConverter.GetEntities(item.Directors);

            if (item.Genres != null)
                entity.Genres = GenreConverter.GetEntities(item.Genres);

            if (item.Countries != null)
                entity.Countries = CountryConverter.GetEntities(item.Countries);

            if (item.Studios != null)
                entity.Studios = StudioConverter.GetEntities(item.Studios);

            return entity;
        }
    }
}
